var DATATIMES;//当前时间全局变量
var DATATIME;//时间的全局变量
var AATIME;//当前时间前六个月
$(function(){
	//得到当前时间
	 var now = new Date();
     var year = now.getFullYear();       //年
     var month = now.getMonth() + 1;     //月
     var clock = year + " ";
     if(month < 10)
     clock += "0";
     clock += month;
     DATATIME=clock;
     DATATIMES=clock;
     
     
     
     
 	var arr1 = DATATIMES.split(' ');
    var year1 = arr1[0]; //获取当前日期的年份
    var month1 = arr1[1]; //获取当前日期的月份
    var days1 = new Date(year1, month1, 0);
    days1 = days1.getDate(); //获取当前日期中月的天数
    var year21 = year1;
    var month21 = parseInt(month1) - 1;
    if (month21 == 0) {
        year21 = parseInt(year21) - 1;
        month21 = 12;
    }
    if (month21 < 10) {
        month21 = '0' + month21;
    }
     AATIME = year21 + ' ' + month21;
     
     for(var i=0;i<4;i++){ 
     	var arr11 = AATIME.split(' ');
         var year11 = arr11[0]; //获取当前日期的年份
         var month11 = arr11[1]; //获取当前日期的月份
         var days11 = new Date(year11, month11, 0);
         days11 = days11.getDate(); //获取当前日期中月的天数
         var year211 = year11;
         var month211 = parseInt(month11) - 1;
         if (month211 == 0) {
             year211 = parseInt(year211) - 1;
             month211 = 12;
         }
         if (month211 < 10) {
             month211 = '0' + month211;
         }
         var t21 = year211 + ' ' + month211;
         AATIME=t21;//上一个月日期
     }
     
     
     $("#CurentTime").append(clock);
     
     //判断当前用户 是否存在绑定账单
	ajaxJsonCall("unifybill/service/UnifyBillAnalysis.lifeBill.json", {},function(data){
		var billList=data.responseData.billList; 	
		if(billList.length ==0){//没有数据
			$("#zdfxshow").hide();
			$("#mybdzd").show();
			
		}else{
			getPie()//如果绑定了账单就加载饼状图
			$("#zdfxshow").show();
			$("#mybdzd").hide();
			
			if(billList.length ==1){
				if(billList[0].type_id=="3"){
					$("#shuiID").show();
					getTreeS();//水树状图和称号
					var guding = document.getElementById ('shuiID');
			        guding.style.background = '#6186FA';
				}else{
					$("#shuiID").hide();
				}
				if(billList[0].type_id=="4"){
					$("#ranqiID").show();
					getTreeTRQ();//燃气树状图
					var guding = document.getElementById ('ranqiID');
			        guding.style.background = '#6186FA';
				}else{
					$("#ranqiID").hide();
				}
				
			}else if(billList.length ==2){
				if(billList[0].type_id=="3"){
					$("#shuiID").show();
					//getTreeS();//水树状图和称号
				}else if(billList[0].type_id=="4"){
					$("#ranqiID").show();
					getTreeTRQ();//燃气树状图
					var guding = document.getElementById ('ranqiID');
			        guding.style.background = '#6186FA';
				}
				if(billList[1].type_id=="4"){
					$("#ranqiID").show();
					getTreeTRQ();//燃气树状图
					var guding = document.getElementById ('ranqiID');
			        guding.style.background = '#6186FA';
				}else if(billList[1].type_id=="3"){
					$("#shuiID").show();
				}
			}
		
		}
		
	});
});

/**
 * 获取上一个月
 */
function getPreMonth() {
    var arr = DATATIME.split(' ');
    var year = arr[0]; //获取当前日期的年份
    var month = arr[1]; //获取当前日期的月份
    var days = new Date(year, month, 0);
    days = days.getDate(); //获取当前日期中月的天数
    var year2 = year;
    var month2 = parseInt(month) - 1;
    if (month2 == 0) {
        year2 = parseInt(year2) - 1;
        month2 = 12;
    }
    if (month2 < 10) {
        month2 = '0' + month2;
    }
    var t2 = year2 + ' ' + month2;
    DATATIME=t2;//得到上一个月日期
    
    
    var arr1=DATATIME.split(' '); //截取得到上一个月的日期
    var starttime=new Date(arr1[0],arr1[1]); 
    var starttimes=starttime.getTime(); ////上一个月日期毫秒数
    
    
    var arr2=AATIME.split(' ');//截取得到当前日期的前六个月
    var starttime2=new Date(arr2[0],arr2[1]); 
    var starttimes2=starttime2.getTime(); ////当前时间的前六个月  毫秒数
    
    
    if (starttimes < starttimes2) {  //山个月日期 小于 当前时间的前六个月日期
    	DATATIME=AATIME;
        return false;  
    }
    if (starttimes == starttimes2) {  //山个月日期  等于 当前时间的前六个月日期
    	var guding = document.getElementById ('syg');
        guding.style.color = '#c1c1c1';
    }
    
    var guding1 = document.getElementById ('xyg');
    guding1.style.color = '#6186FA';
   
    $("#CurentTime").html(DATATIME);//饼状图日期显示
    getPie();//饼状图
  
}

/**
 * 获取下一个月
 */        
function getNextMonth() {
    var arr = DATATIME.split(' ');
    var year = arr[0]; //获取当前日期的年份
    var month = arr[1]; //获取当前日期的月份
    var days = new Date(year, month, 0);
    days = days.getDate(); //获取当前日期中的月的天数
    var year2 = year;
    var month2 = parseInt(month) + 1;
    if (month2 == 13) {
        year2 = parseInt(year2) + 1;
        month2 = 1;
    }
    if (month2 < 10) {
        month2 = '0' + month2;
    }

    var t2 = year2 + ' ' + month2 ;
    DATATIME=t2;
    
    
    var arr1=DATATIME.split(' '); //截取得到下一个月的日期
    var starttime=new Date(arr1[0],arr1[1]); 
    var starttimes=starttime.getTime(); ////上一个月日期毫秒数
    
    
    var arr2=DATATIMES.split(' ');//当前日期
    var starttime2=new Date(arr2[0],arr2[1]); 
    var starttimes2=starttime2.getTime(); ////当前日期 的前六个月  毫秒数
    
    if (starttimes > starttimes2) {  
    	DATATIME=DATATIMES;
        return false;  
    }  
    if (starttimes > starttimes2 ) {  
    	var guding = document.getElementById ('xyg');
        guding.style.color = '#c1c1c1';
    } 
    var guding1 = document.getElementById ('syg');
    guding1.style.color = '#6186FA';
    $("#CurentTime").html(DATATIME);
    getPie();//饼状图
   
}

function getPie(){//饼状图
	ajaxJsonCall("unifybill/service/UnifyBillAnalysis.getbillAnalysisPie.json", {times:DATATIME},function(data){
		var dataList=data.responseData.dataList; 
		var data = [];
		if(dataList.length ==0){//没有数据
			document.getElementById('container11').innerHTML='暂无数据!';
			$("#pieTable").append("<tr>"+"<td>总计</td>"+
					  "<td>"+parseFloat(shouldPayTotal).toFixed(2)+"</td>"+
					  "<td>"+Percentage+'%'+"</td>"+
					  "<td>--</td>"+
					"</tr>");
			$("#pieTable tr:gt(0)").remove();
			}else{
				for(var i=0;i<dataList.length;i++){
					var objaa=[];
					objaa.push(String(dataList[i].type_name));
					objaa.push(Number(dataList[i].shouldPay));
					data.push(objaa)
			}
				
				$('#container11').highcharts({//饼状图
					        chart: {
					            plotBackgroundColor: null,
					            plotBorderWidth: null,
					            plotShadow: false
					        },
					        title: {
					            text: ' '
					        },
					        credits: {
					            enabled:false
					         },
					        tooltip: {
					    	    pointFormat: '{series.name}: <b>{point.percentage:.0f}%</b>'
					        },
					        plotOptions: {
					            pie: {
					                allowPointSelect: true,
					                cursor: 'pointer',
					                dataLabels: {
					                    enabled: false,
					                    color: '#000000',
					                    connectorColor: '#000000'
					                    	,
					                    format: '<b>{point.name}</b>: {point.percentage:.0f} %'
					                }
					            }
					        },
					        series: [{
					            type: 'pie',
					            name: '比例',
					            data: data
					        }]
					    });	
				$("#pieTable tr:gt(0)").remove();
				var shouldPayTotal =0;//总计
				var Percentage = 0;//百分比
			for(var i=0;i<dataList.length;i++){
				shouldPayTotal+=parseFloat(dataList[i].shouldPay);
				Percentage+=parseFloat(dataList[i].percentage);
			}
			var path = document.getElementById("tupuid").value;
			$("#pieTable").append("<tr>"+"<td>总计</td>"+
					  "<td>"+parseFloat(shouldPayTotal).toFixed(2)+"</td>"+
					  "<td>"+Percentage+'%'+"</td>"+
					  "<td>--</td>"+
					"</tr>");
			for(var i=0;i<dataList.length;i++){
				var avgtb =dataList[i].avgtb;
				if(avgtb==1){
					var tp = "public/unifybill/service/imgaes/up.png";
				}else if(avgtb==2){
					var tp = "public/unifybill/service/imgaes/bottom.png";
				}
			
				$("#pieTable").append("<tr>"+"<td>"+dataList[i].type_name+"</td>"+
										  "<td>"+dataList[i].shouldPay+"</td>"+
										  "<td>"+dataList[i].percentage+'%'+"</td>"+
										  "<td>"+"<img id='tp' src='"+app_path+'/'+tp+"'/>"+"</td>"+
										"</tr>");
			}
			}
		
			});
	};




function getTreeTRQ(){//点击天然气 按钮  柱状图  加载#6186FA
	var guding = document.getElementById ('ranqiID');
    guding.style.background = '#6186FA';
    var gudings = document.getElementById ('shuiID');
    gudings.style.background = '#c1c1c1';
	ajaxJsonCall("unifybill/service/UnifyBillAnalysis.getBillTreeTRQ.json", {},function(data){
		var dataListTreeTRQ=data.responseData.dataListTreeTRQ; 
		var data1 = [];// X轴 
		var data2 = []; //Y轴
			for(var i=0;i<dataListTreeTRQ.length;i++){
				var obj1=[];
				var obj2=[];
				obj1.push(dataListTreeTRQ[i].copyDate.substring(0,4)+'/'+dataListTreeTRQ[i].copyDate.substring(4,6));
				obj2.push(Number(dataListTreeTRQ[i].shouldPay));
				data1.push(obj1);
				data2.push(obj2)
		}
		$('#container1111').highcharts({
	        chart: {
	            type: 'column'
	        },
	        title: {
	            text: ''
	        },
	        subtitle: {
	            text: ''
	        },
	        credits: {
	            enabled:false
	         },
	         legend: {
	             enabled:false
	          },
	        xAxis: {
	            categories: data1,
	            title: {
	                text: '时间',
	                style:{fontSize:"14",fontWeight:""}
	            }
	        },
	        yAxis: {
	            min: 0,
	            title: {
	                text: '金额 (元)',
	                style:{fontSize:"14",fontWeight:""}
	            }
	        },
	        tooltip: {
	            pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
	                '<td style="padding:0"><b>{point.y:.1f}(元)</b></td></tr>',
	            shared: false,
	            useHTML: false
	        },
	        plotOptions: {
	            column: {
	                pointPadding: 0.2,
	                borderWidth: 0
	            }
	        },
	        series: [{
	            name: String(dataListTreeTRQ[0].type_name),
	            data: data2,
	            pointWidth:30 //每一根柱子宽度

	        }]
	    });
	});
	ajaxJsonCall("unifybill/service/UnifyBillAnalysis.getBillTreeTRQTitle.json", 
			{}, function(data){
				var path = document.getElementById("tupuid").value;
				var billTreeTRQTitle=data.responseData.billTreeTRQTitle; 
				 var a1=document.getElementById ("aa1");
				  if(billTreeTRQTitle.length ==0){
					 a1.innerHTML = "勤俭持家称号！太节俭！赶快结束单身汪(gou)的生活吧！";
					 document.getElementById('aa2').src =app_path+"/public/unifybill/service/imgaes/name_3.png";
				 }else if(billTreeTRQTitle[0].title==3){
					 a1.innerHTML = "勤俭持家称号！太节俭！赶快结束单身汪(gou)的生活吧！";
					 document.getElementById('aa2').src =app_path+"/public/unifybill/service/imgaes/name_3.png";
				 }else  if(billTreeTRQTitle[0].title==2){
					 a1.innerHTML = "勤劳小蜜蜜称号！你超越了50%的成都市民！你这天天的洗澡,做饭,你是冲着三好标兵去的吗？";
					 document.getElementById('aa2').src =app_path+"/unibill/public/unifybill/service/imgaes/name_2.png";
				 }else  if(billTreeTRQTitle[0].title==1){
					 a1.innerHTML = "土豪就是任性称号！你超越了80%的成都市民！你这么任性，你未来/现在的老婆（老公）知道吗？";
					 document.getElementById('aa2').src =app_path+"/public/unifybill/service/imgaes/name_1.png";
				 }
				});
}



function getTreeS(){//水  柱状图  加载
	var gudings = document.getElementById ('shuiID');
    gudings.style.background = '#6186FA';
    var guding = document.getElementById ('ranqiID');
    guding.style.background = '#c1c1c1';
	ajaxJsonCall("unifybill/service/UnifyBillAnalysis.getBillTreeS.json", {},function(data){
		var dataListTreeS=data.responseData.dataListTreeS; 
		
		var data1 = [];// X轴 
		var data2 = []; //Y轴
			for(var i=0;i<dataListTreeS.length;i++){
				var obj1=[];
				var obj2=[];
				obj1.push(dataListTreeS[i].recordDate.substring(0,4)+'/'+dataListTreeS[i].recordDate.substring(5,7));
				obj2.push(Number(dataListTreeS[i].shouldPay));
				data1.push(obj1);
				data2.push(obj2)
		}
		$('#container1111').highcharts({
	        chart: {
	            type: 'column'
	        },
	        title: {
	            text: ''
	        },
	        subtitle: {
	            text: ''
	        },
	        credits: {
	            enabled:false
	         },
	         legend: {
	             enabled:false
	          },
	        xAxis: {
	            categories: data1,
	            title: {
	                text: '时间',
	                style:{fontSize:"14",fontWeight:""}
	            }
	        },
	        yAxis: {
	            min: 0,
	            font:"0",
	            title: {
	                text: '金额 （元）',
	                style:{fontSize:"14",fontWeight:""}
	            }
	        	
	        },
	        tooltip: {
	            pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
	                '<td style="padding:0"><b>{point.y:.1f}(元)</b></td></tr>',
	            shared: false,
	            useHTML: false
	        },
	        plotOptions: {
	            column: {
	                pointPadding: 0.2,
	                borderWidth: 0
	            }
	        },
	        series: [{
	            name: String(dataListTreeS[0].type_name),
	            data: data2,
	            pointWidth:30 //每一根柱子宽度

	        }]
	    });
	});
		ajaxJsonCall("unifybill/service/UnifyBillAnalysis.getBillTreeSTitle.json", 
		{}, function(data){
			var path = document.getElementById("tupuid").value;
			var billTreeSTitle=data.responseData.billTreeSTitle; 
			 var a1=document.getElementById ("aa1");
			  if(billTreeSTitle.length ==0){
				 a1.innerHTML = "勤俭持家称号！太节俭！赶快结束单身汪(gou)的生活吧！";
				 document.getElementById('aa2').src =path+"public/unifybill/service/imgaes/name_3.png";
			 }else if(billTreeSTitle[0].title==3){
				 a1.innerHTML = "勤俭持家称号！太节俭！赶快结束单身汪(gou)的生活吧！";
				 document.getElementById('aa2').src =path+"public/unifybill/service/imgaes/name_3.png";
			 }else  if(billTreeSTitle[0].title==2){
				 a1.innerHTML = "勤劳小蜜蜜称号！你超越了50%的成都市民！你这天天的洗澡,做饭,你是冲着三好标兵去的吗？";
				 document.getElementById('aa2').src =path+"public/unifybill/service/imgaes/name_2.png";
			 }else  if(billTreeSTitle[0].title==1){
				 a1.innerHTML = "土豪就是任性称号！你超越了80%的成都市民！你这么任性，你未来/现在的老婆（老公）知道吗？";
				 document.getElementById('aa2').src =path+"public/unifybill/service/imgaes/name_1.png";
			 }
				});
}


  